package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.activity.ActivityTransfer;
import com.admin.education.dataTransferObject.activity.Request.CreateActivityRequest;
import com.admin.education.dataTransferObject.activity.Request.UpdateActivityRequest;
import com.admin.education.services.Interfaces.IActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@JwtAuthorize
@RestController(value = "adminActivityController")
@RequestMapping("activitys")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ActivityController {
    @Autowired
    private IActivityService activityService;

    @GetMapping("")
    public Callable<IResponseMessage> queryActivitys(@Valid PageRequest pageRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<PageResponse<ActivityTransfer>> servicesExcuteResult = activityService.queryActivities(pageRequest);

                if (servicesExcuteResult.isSuccess()) {
                    responseMessage = new ResponseMessageWithData<PageResponse<ActivityTransfer>>(Result.Success, servicesExcuteResult.getData());
                } else {
                    throw new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @GetMapping("{activityId}")
    public Callable<IResponseMessage> queryActivityById(@PathVariable Long activityId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<ActivityTransfer> result = activityService.queryActivityById(activityId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessageWithData<ActivityTransfer>(Result.Success, result.getData());
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PostMapping("")
    public Callable<IResponseMessage> createActivity(@RequestBody CreateActivityRequest createActivityRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = activityService.createActivity(createActivityRequest);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @DeleteMapping("{activityId}")
    public Callable<IResponseMessage> deleteActivityById(@PathVariable Long activityId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = activityService.deleteActivity(activityId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PutMapping("{activityId}")
    public Callable<IResponseMessage> updateActivity(@PathVariable Long activityId, @RequestBody @Valid UpdateActivityRequest updateActivityRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = activityService.updateActivity(activityId, updateActivityRequest);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PutMapping("{activityId}/Valid")
    public Callable<IResponseMessage> updateActivityValid(@PathVariable Long activityId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = activityService.setValudActivity(activityId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
